package br.com.sistema.views;

import br.com.sistema.util.Toolbar;

import com.vaadin.data.Container;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public abstract class AbstractView extends Window {
	
	private static final long serialVersionUID = 1L;
	
	private MenuBar menubar = new MenuBar();
	private Toolbar toolbar = new Toolbar();
	private Component component;
	private Panel panelSearch = new Panel();
	private Table table = new Table();
	private Button pesquisa = new Button(Caption.PESQUISA);
	//private Button ocultaPesquisa = new Button("Ocultar Pesquisa");
	//private Button exibePesquisa = new Button("Exibir Pesquisa");
	
	public Toolbar getToolbar() {
		return toolbar;
	}
	
	public MenuBar getMenubar() {
		return menubar;
	}
	
	public Component getComponent() {
		return component;
	}
	
	public Table getTable(){
		return this.table;
	}
	
	public Window getView(){
		return this;
	}
	
	public Panel getPanelSearch(){
		return this.panelSearch;
	}
	
	public Button getPesquisa() {
		return pesquisa;
	}
	
	public abstract Object[] getVisibleColumns();
	public abstract String[] getColumnHeaders();
	public abstract Container getBeanContainer();
	public abstract Component getSearchArea();
	
	protected void init(){

		//toolbar.setMargin(new MarginInfo(true, false, true, false));
		
		table.setSizeFull();
		table.setContainerDataSource(getBeanContainer());
		table.setImmediate(true);
		table.setSelectable(true);
		table.setMultiSelect(true);
		
		table.setVisibleColumns(getVisibleColumns());
		table.setColumnHeaders(getColumnHeaders());
		
		menubar.setWidth(100, Unit.PERCENTAGE);
		
		final VerticalLayout conteudoPainelPesquisa = new VerticalLayout();
		conteudoPainelPesquisa.setMargin(new MarginInfo(true, true, true, true));
		conteudoPainelPesquisa.setSpacing(true);
		conteudoPainelPesquisa.addComponent(this.getSearchArea());
		
		HorizontalLayout h0 = new HorizontalLayout();
		h0.setSpacing(true);
		h0.addComponent(this.pesquisa);
		//h0.addComponent(this.ocultaPesquisa);
		conteudoPainelPesquisa.addComponent(h0);
		
		//panelSearch.setCaption("Pesquisa");
		panelSearch.setContent(conteudoPainelPesquisa);
		
		component = new GridLayout();		
		component.setSizeFull();
		((GridLayout) component).setMargin(false);
		((GridLayout) component).setColumns(1);
		((GridLayout) component).setRows(4);
		((GridLayout) component).addComponent(menubar, 0, 0);
		((GridLayout) component).addComponent(panelSearch, 0, 1);
		((GridLayout) component).addComponent(toolbar, 0, 2);
		((GridLayout) component).addComponent(table, 0, 3);
		((GridLayout) component).setRowExpandRatio(3, 1);
		
		this.setContent(component);
		setWidth(700, Unit.PIXELS);
		setHeight(500, Unit.PIXELS);
		
		// a pesquisa inicia-se invisivel
		getPanelSearch().setVisible(false);
		
		this.setCloseShortcut(KeyCode.ESCAPE, null);
		
		toolbar.getPesquisa().addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void buttonClick(ClickEvent event) {
				if(getPanelSearch().isVisible()){
					getPanelSearch().setVisible(false);
				}else{
					getPanelSearch().setVisible(true);
				}
			}
		});
		
		this.addFocusListener(new FocusListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void focus(FocusEvent event) {
				Page.getCurrent().setTitle(getCaption());
			}
		});
		
		this.addCloseListener(new CloseListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void windowClose(CloseEvent e) {
				//TODO pegar titulo generico
				Page.getCurrent().setTitle("Sistema");
			}
		});
		
		// TODO verificar o focus da janela para alterar o nome da janela
	}
	
}
